import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

//页面跳转到相同路由不会报错(重写replace)
const originalPush = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace(location) {
    return originalPush.call(this, location).catch(err => err)
}

const routes = [
  {
    path:'/',
    name:'homepage',
    component:() => import('../views/homepage.vue'),
    meta:{
      title:'首页'
    }
  },
  {
    path: '*',
    name: 'notfound',
    component: () => import('../views/notfound.vue'),
    meta:{
      title:'错误页面'
    }
  },
  {
    path:'/login',
    name:'login',
    component:() => import('../views/login.vue'),
    meta:{
      title:'登录'
    }
  },
  {
    path:'/register',
    name:'register',
    component:() => import('../views/register.vue'),
    meta:{
      title:'注册'
    }
  },

  {
    path:'/recharge',
    name:'recharge',
    component:() => import('../views/recharge.vue'),
    meta:{
        flag:true,
        title:'会员专享'
    }
  },
  {
    path:'/setup',
    name:'setup',
    component:() => import('../views/setup.vue'),
    children:[
      {
        path:'/download',
        name:'download',
        component:() => import('../components/setup/download.vue'),
        meta:{
          title:'设置'
        },
      },
      {
        path:'/safe',
        name:'safe',
        component:() => import('../components/setup/safe.vue'),
        meta:{
          title:'设置'
        },
      },
      {
        path:'/popup',
        name:'popup',
        component:() => import('../components/setup/popup.vue'),
        meta:{
          title:'设置'
        },
      },
      {
        path:'/plupinunit',
        name:'plupinunit',
        component:() => import('../components/setup/plupinunit.vue'),
        meta:{
          title:'设置'
        },
      },
      {
        path:'/other',
        name:'other',
        component:() => import('../components/setup/other.vue'),
        meta:{
          title:'设置'
        },
      },
    ]
  },
  
  
  {
    path:'/servise',
    name:'servise',
    component:() => import('../views/servise.vue'),
    meta:{
      title:'关于我们'
    }
  },
  {
    path:'/details',
    name:'details',
    component:() => import('../views/details.vue'),
    children:[
      {
        path:'/privacy',
        name:'privacy',
        meta:{
          title:'网站简介'
        },
        component:() => import('../components/details/privacy')
      },
      {
        path:'/search',
        name:'search',
        meta:{
          title:'网站简介'
        },
        component:() => import('../components/details/search')
      },
      {
        path:'/service',
        name:'service',
        meta:{
          title:'网站简介'
        },
        component:() => import('../components/details/service')
      },
    ]
  },
  {
    path:'/personal',
    name:'personal',
    component:() => import('../views/personal.vue'),
    meta:{
      flag:true,
    },
    children:[
      {
        path:'/collect',
        name:'collect',
        component:() => import('../components/personal/collect'),
        meta:{
          flag:true,
          title:'个人主页'
        },
      },
      {
        path:'/follow',
        name:'follow',
        component:() => import('../components/personal/follow'),
        meta:{
          flag:true,
          title:'个人主页'
        },
      },
      {
        path:'/history',
        name:'history',
        component:() => import('../components/personal/history'),
        meta:{
          flag:true,
          title:'个人主页'
        },
      },
      {
        path:'/modify',
        name:'modify',
        component:() => import('../components/personal/modify'),
        meta:{
          flag:true,
          title:'个人主页'
        },
      },
      {
        path:'/person',
        name:'person',
        component:() => import('../components/personal/person'),
        meta:{
          flag:true,
          title:'个人主页'
        },
      },
      {
        path:'/release',
        name:'release',
        component:() => import('../components/personal/release'),
        meta:{
          flag:true,
          title:'个人主页'
        },
      },
      {
        path:'/fans',
        name:'fans',
        component:() => import('../components/personal/fans'),
        meta:{
          flag:true,
          title:'个人主页'
        },
      },
      {
        path:'/contribute',
        name:'contribute',
        component:() => import('../components/personal/contribute.vue'),
        meta:{
          flag:true,
          title:'个人主页'
        },
      },
    ]
  },
  {
    path:'/result',
    name:'result',
    component:() => import('../views/result.vue'),
    meta:{
      title:'搜索结果'
    },
  },
  {
    path:'/video',
    name:'video',
    component:() => import('../views/video.vue'),
    meta:{
      flag:true,
      title:'视频界面'
    },
  },
  {
    path:'/message',
    name:'message',
    component:() => import('../views/message.vue'),
    meta:{
      flag:true,
      title:'聊天'
    },
  },
  {
    path:'/classification',
    name:'classification',
    component:() => import('../views/classification.vue'),
    meta:{
      flag:true,
      title:'分类'
    },
  },
  {
    path:'/friends_interface',
    name:'friends_interface',
    component:() => import('../views/friends_interface.vue'),
    meta:{
      flag:true,
    },
    children:[
      {
        path:'/friend_person',
        name:'friend_person',
        component:() => import('../components/friends_interface/friend_person.vue'),
        meta:{
          flag:true,
          title:'好友主页'
        },
      },
      {
        path:'/friend_collect',
        name:'friend_collect',
        component:() => import('../components/friends_interface/friend_collect.vue'),
        meta:{
          flag:true,
          title:'好友主页'
        },
      },
      {
        path:'/friend_fans',
        name:'friend_fans',
        component:() => import('../components/friends_interface/friend_fans.vue'),
        meta:{
          flag:true,
          title:'好友主页'
        },
      },
      {
        path:'/friend_follow',
        name:'friend_follow',
        component:() => import('../components/friends_interface/friend_follow.vue'),
        meta:{
          flag:true,
          title:'好友主页'
        },
      },
      {
        path:'/friend_release',
        name:'friend_release',
        component:() => import('../components/friends_interface/friend_release.vue'),
        meta:{
          flag:true,
          title:'好友主页'
        },
      },
    ]
  },
  {
    path:'/activity',
    name:'activity',
    component:() => import('../views/activity.vue'),
    meta:{
      title:'活动页面'
    }
  },
]

const router = new VueRouter({
  mode: 'history',
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
        return savedPosition
    } else {
        return {x: 0, y: 0}
    }
},
  routes
})

//防止用户非法进入页面
router.beforeEach((to,from,next) => {
  if(to.meta.flag===true){
      if(sessionStorage.getItem('token') != null){
          next()
      }else{
          next({path:'/login'})
      }
  }else{
      next()
  }
})

//重写push方法，防止带参跳转页面报错
let originPush =  VueRouter.prototype.push;  //备份原push方法 
VueRouter.prototype.push = function (location, resolve, reject){
    if (resolve && reject) {    //如果传了回调函数，直接使用
        originPush.call(this, location, resolve, reject);
    }else {                     //如果没有传回调函数，手动添加
        originPush.call(this, location, ()=>{}, ()=>{}); 
    }
}

export default router
